*Scatterplots cohold amount (in exp. days) against cohold length
*Any coholder
*Individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding.png", width(1800) height(1300) replace

*Single individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==0, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_S.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_S.png", width(1800) height(1300) replace

*Linked individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_L.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_L.png", width(1800) height(1300) replace

*Household x spell
binscatter cohold_amount_exp_HH cohold_length_HH if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1 & hh_edate_obs==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_HH.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_HH.png", width(1800) height(1300) replace


*For frequent coholders:
*Individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & frequent_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_Frequent.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_Frequent.png", width(1800) height(1300) replace

*Single individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==0  & frequent_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_S_Frequent.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_S_Frequent.png", width(1800) height(1300) replace

*Linked individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1  & frequent_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_L_Frequent.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_L_Frequent.png", width(1800) height(1300) replace

*Household x spell
binscatter cohold_amount_exp_HH cohold_length_HH if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1 & hh_edate_obs==1 & frequent_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_HH_Frequent.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_HH_Frequent.png", width(1800) height(1300) replace


*For intense coholders:
*Individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & intense_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_Intense.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_Intense.png", width(1800) height(1300) replace

*Single individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==0  & intense_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_S_Intense.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_S_Intense.png", width(1800) height(1300) replace

*Linked individual x spell
binscatter cohold_amount_exp cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1  & intense_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_L_Intense.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_L_Intense.png", width(1800) height(1300) replace

*Household x spell
binscatter cohold_amount_exp_HH cohold_length_HH if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1 & hh_edate_obs==1  & intense_coholder==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (in consumption days)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholding_HH_Intense.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholding_HH_Intense.png", width(1800) height(1300) replace


*Scatterplots cohold amount (divided by individual-specific monthly income) against cohold length
*Individual x spell
binscatter cohold_amount_ISminc cohold_length if coholder==1 & ind_spell_obs==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (divided by monthly income)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0) format(%9.1fc)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholdingInc2.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholdingInc2.png", width(1800) height(1300) replace

*Single individual x spell
binscatter cohold_amount_ISminc cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==0, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (divided by monthly income)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0) format(%9.1fc)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholdingInc2_S.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholdingInc2_S.png", width(1800) height(1300) replace

*Linked individual x spell
binscatter cohold_amount_ISminc cohold_length if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (divided by monthly income)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0) format(%9.1fc)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholdingInc2_L.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholdingInc2_L.png", width(1800) height(1300) replace

*Household x spell
binscatter cohold_amount_ISminc_HH cohold_length_HH if coholder==1 & ind_spell_obs==1 & alwaysin2memberhh==1 & hh_edate_obs==1, ///
mcolors(blue) ///
line(qfit) lcolor(blue) ///
title("Average amount of coholding (divided by monthly income)", place(left) size(large)) ///
ytitle("") ///
xtitle("Coholding duration (in days)", size(large)) ///
nquantiles(40) ///
ylabel(, labsize(medium) angle(0) format(%9.1fc)) ///
xlabel(, labsize(medium)) 
graph export "Figures/BinscatterDurationAmountCoholdingInc2_HH.pdf", replace
graph export "Figures/BinscatterDurationAmountCoholdingInc2_HH.png", width(1800) height(1300) replace



********************************************************************************
*Inertia / inattention hypothesis: Does cohold amount decrease when coholding is going on?

*******
*Overdraft cash and unnecessary overdraft in consumption days
foreach y in od_exp_spell cons1_spell unnec_od_exp_spell  {

	if "`y'" == "od_exp_spell" {
	local title "Overdraft in consumption days"
	}
	else if "`y'" == "cons1_spell" {
	local title "Cash in consumption days"
	}
	else if "`y'" == "unnec_od_exp_spell" {
	local title "Unnecessary overdraft in consumption days"
	}


	local category "cohold_spell_cat cohold_spell_cat_frequent cohold_spell_cat_intense"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

	foreach var in `category' {
		if "`var'" == "cohold_spell_cat" | "`var'" == "cohold_spell_cat_frequent" | "`var'" == "cohold_spell_cat_intense" {
			local min 0
			local max 5
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1 & cohold_spell_cat_uoa==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 mcolor(blue) mstyle(p1) ///
	 title("`title'", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("")  ///
	 xline(7 14, lpattern(dot)) ///
	 ylabel(, labsize(medium) angle(0)) /// 
	 xlabel(0.7 `""Coholder:" "No cohold day""' 	     2 "Start" 3 "First 1/3" 4 "Intermediate 1/3" 5 "Last 1/3" 6 "End" ///
		7.7 `""Frequent coholder:" "No cohold day""' 9 "Start" 10 "First 1/3" 11 "Intermediate 1/3" 12 "Last 1/3" 13 "End" ///
		14.7 `""Intense coholder:" "No cohold day""' 16 "Start" 17 "First 1/3" 18 "Intermediate 1/3" 19 "Last 1/3" 20 "End", ///
		labsize(medium) angle(90))  ///
	 xscale(range(0.25 20.75)) ///
	 legend(off) 	 
	 
drop subcategory mean lqt uqt n 
drop min max  	 
graph export "Figures/CohExpl_Inattention_`y'.pdf", replace
graph export "Figures/CohExpl_Inattention_`y'.png", replace

}

*************


foreach y in unnec_od_exp_high unnec_od_exp_low {

	if "`y'" == "unnec_od_exp_high" {
	local title "Large unnecessary overdraft in consumption days"
	}
	else if "`y'" == "unnec_od_exp_low" {
	local title "Low unnecessary overdraft in consumption days"
	}

	local category "cohold_spell_cat cohold_spell_cat_frequent cohold_spell_cat_intense"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

	foreach var in `category' {
		if "`var'" == "cohold_spell_cat" | "`var'" == "cohold_spell_cat_frequent" | "`var'" == "cohold_spell_cat_intense" {
			local min 1 //ignore no cohold day because no observations
			local max 5
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1 & cohold_spell_cat_uoa==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 mcolor(blue) mstyle(p1) ///
	 title("`title'", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("")  ///
	 xline(6 12, lpattern(dot)) ///
	 ylabel(, labsize(medium) angle(0)) /// 
	 xlabel(0.7 `""Coholder:" "Start""' 2 "First 1/3" 3 "Intermediate 1/3" 4 "Last 1/3" 5 "End" ///
		6.7 `""Frequent coholder:" "Start""' 8 "First 1/3" 9 "Intermediate 1/3" 10 "Last 1/3" 11 "End" ///
		12.7 `""Intense coholder:" "Start""' 14 "First 1/3" 15 "Intermediate 1/3" 16 "Last 1/3" 17 "End", ///
		labsize(medium) angle(90))  ///
	 xscale(range(0.25 17.75)) ///
	 legend(off)  
	 
	 
drop subcategory mean lqt uqt n 
drop min max  	 
graph export "Figures/CohExpl_Inattention_`y'.pdf", replace
graph export "Figures/CohExpl_Inattention_`y'.png", replace

}


********************************************************************************
*Inattention hypothesis: Probability of logging in


bysort ind_id: egen mean_total_logins = mean(total_logins)
gen above_median_logins = 0
summarize mean_total_logins if edate_id==1335, detail
return list
local median = r(p50)
replace above_median_logins = 1 if mean_total_logins >= `median'

eststo a: estpost summarize ///
coholder frequent_coholder intense_coholder costly_coholder ///
if above_median_logins == 0 & edate_id==1335, detail

eststo b: estpost summarize ///
coholder frequent_coholder intense_coholder costly_coholder ///
if above_median_logins == 1 & edate_id==1335, detail

eststo diff: quietly estpost ttest ///
coholder frequent_coholder intense_coholder costly_coholder ///
if above_median_logins==1 & edate_id==1335 | above_median_logins==0 & edate_id==1335, by(above_median_logins)

esttab a b diff using "Tables/Coh_Expl_InattentionTtest1.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.2fc)) p25(label(P25) pattern(1 1 0) fmt(%15.2fc)) p50(label(P50) pattern(1 1 0) fmt(%15.2fc)) p75(label(P75) pattern(1 1 0) fmt(%15.2fc)) p90(label(P90) pattern(1 1 0) fmt(%15.2fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.2fc)) se(label(SE) par pattern(0 0 1) fmt(%15.2fc))") ///
mtitle("Below median logins" "Above median logins" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
coholder "Coholder" frequent_coholder "Frequent coholder" ///
intense_coholder "Intense coholder" ///
costly_coholder "Costly coholder" ///
) ///
substitute(? \)


eststo a: estpost summarize ///
cohold_day pr_cohold_start pr_cohold_end ///
if total_logins == 0 & coholder==1, detail

eststo b: estpost summarize ///
cohold_day pr_cohold_start pr_cohold_end ///
if total_logins == 1 & coholder==1, detail

eststo diff: quietly estpost ttest ///
cohold_day pr_cohold_start pr_cohold_end ///
if total_logins==1 & coholder==1 | total_logins==0 & coholder==1, ///
by(total_logins)

esttab a b diff using "Tables/Coh_Expl_InattentionTtest2.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.2fc)) p25(label(P25) pattern(1 1 0) fmt(%15.2fc)) p50(label(P50) pattern(1 1 0) fmt(%15.2fc)) p75(label(P75) pattern(1 1 0) fmt(%15.2fc)) p90(label(P90) pattern(1 1 0) fmt(%15.2fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.2fc)) se(label(SE) par pattern(0 0 1) fmt(%15.2fc))") ///
mtitle("No logins" "Logins" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
cohold_day "Cohold day" pr_cohold_start "Coholding starts" pr_cohold_end "Coholding ends" ///
) ///
substitute(? \)



foreach y in total_logins_spell {

	local category "cohold_spell_cat cohold_spell_cat_frequent cohold_spell_cat_intense cohold_spell_cat_costly cohold_spell_cat_high cohold_spell_cat_low"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

	foreach var in `category' {
			
		if "`var'" == "cohold_spell_cat" | "`var'" == "cohold_spell_cat_frequent" | "`var'" == "cohold_spell_cat_intense" | "`var'" == "cohold_spell_cat_costly" {
			local min 0
			local max 5
		} 
		else if "`var'" == "cohold_spell_cat_high" | "`var'" == "cohold_spell_cat_low" {
			local min 1 //no observations for 0
			local max 5
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1 & cohold_spell_cat_uoa==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 title("Probability of logging in bank app", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("", size(large))  ///
	 mcolor(blue) mstyle(p1) ///
	 xline(7 14 21 28 34, lpattern(dot)) ///
	 ylabel(, labsize(medium) angle(0) format(%9.2fc)) /// 
	 xlabel( 0.53 `""Coholder:" "No cohold day""' 		2 "Start"  3 "First 1/3"  4 "Intermediate 1/3"  5 "Last 1/3"  6 "End" ///
		 7.53 `""Frequent coholder:" "No cohold day""' 	9 "Start" 10 "First 1/3" 11 "Intermediate 1/3" 12 "Last 1/3" 13 "End" ///
		14.53  `""Intense coholder:" "No cohold day""' 16 "Start" 17 "First 1/3" 18 "Intermediate 1/3" 19 "Last 1/3" 20 "End" ///
		21.53  `""Costly coholder:" "No cohold day""' 23 "Start" 24 "First 1/3" 25 "Intermediate 1/3" 26 "Last 1/3" 27 "End" ///
		28.53 `""High amounts:" "Start""' 			  30 "First 1/3" 31 "Intermediate 1/3" 32 "Last 1/3" 33 "End" ///
		34.53 `""Low amounts:" "Start""' 			  36 "First 1/3" 37 "Intermediate 1/3" 38 "Last 1/3" 39 "End", ///
		 labsize(small) angle(90)) ///
	 xscale(range(0.25 39.75)) ///
	 legend(off)	 
	 
drop subcategory mean lqt uqt n 
drop min max  	 
graph export "Figures/CohExpl_Inattention_`y'.pdf", replace
graph export "Figures/CohExpl_Inattention_`y'.png", replace

}



********************************************************************************

*Inattention / Inertia hypothesis: Log in, payday, weekends


gen within_15_after_pay_day = 0
replace within_15_after_pay_day = 1 if pay_day==1
replace within_15_after_pay_day = 1 if ///
		pay_day[_n-1] ==1 & ind_id==ind_id[_n-1] | ///
		pay_day[_n-2] ==1 & ind_id==ind_id[_n-2] | ///
		pay_day[_n-3] ==1 & ind_id==ind_id[_n-3] | ///
		pay_day[_n-4] ==1 & ind_id==ind_id[_n-4] | ///
		pay_day[_n-5] ==1 & ind_id==ind_id[_n-5] | ///
		pay_day[_n-6] ==1 & ind_id==ind_id[_n-6] | ///
		pay_day[_n-7] ==1 & ind_id==ind_id[_n-7] | ///
		pay_day[_n-8] ==1 & ind_id==ind_id[_n-8] | ///
		pay_day[_n-9] ==1 & ind_id==ind_id[_n-9] | ///
		pay_day[_n-10] ==1 & ind_id==ind_id[_n-10] | ///
		pay_day[_n-11] ==1 & ind_id==ind_id[_n-11] | ///
		pay_day[_n-12] ==1 & ind_id==ind_id[_n-12] | ///
		pay_day[_n-13] ==1 & ind_id==ind_id[_n-13] | ///
		pay_day[_n-14] ==1 & ind_id==ind_id[_n-14] | ///
		pay_day[_n-15] ==1 & ind_id==ind_id[_n-15] 
		
		
		
	

*Probability of coholding period ending
foreach y in pr_cohold_start pr_cohold_end {

	if "`y'" == "pr_cohold_start" {
	local title "Probability of coholding period starting"
	}
	if "`y'" == "pr_cohold_end" {
	local title "Probability of coholding period ending"
	}
	
	local category "weekend pay_day second_half_month"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

		foreach var in `category' {
		if "`var'" == "weekend" | "`var'" == "payday" | "`var'" == "second_half_month" {
			local min 0
			local max 1
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 mcolor(blue) mstyle(p1) ///
	 title("`title'", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("")  ///
	 xline(3 6 9, lpattern(dot)) ///
	 ylabel(, labsize(medium) angle(0) format(%9.3fc)) /// 
	 xlabel(1 `""Coholder:" "No weekend""' 2 "Weekend" ///
		 4  `""Coholder:" "No payday""' 5 "Payday" ///
		 7 `""Coholder:" "Outside 15 days after payday""' 8 "Within 15 days after payday", ///
	 labsize(medium) angle(90)) ///
	 xscale(range(0.25 8.75)) ///
	 legend(off) 
	 
drop subcategory mean lqt uqt n 
drop min max  	 
graph export "Figures/CohExpl_Inattention_`y'.pdf", replace
graph export "Figures/CohExpl_Inattention_`y'.png", replace
}

*Probability of start or end by month
foreach var in pr_cohold_start_month pr_cohold_end_month {

bysort month: egen mean = mean(`var') if coholder==1 & ind_emy_seq==1
bysort month: egen std = sd(`var') if coholder==1 & ind_emy_seq==1
bysort month: egen obs = count(dum) if coholder==1 & ind_emy_seq==1
gen se = std/sqrt(obs) if coholder==1 & ind_emy_seq==1
bysort month: egen n = seq()  if coholder==1 & ind_emy_seq==1

	if "`var'" == "pr_cohold_start_month" {
	local title "Probability of cohold period starting"
	}
	else if "`var'" == "pr_cohold_end_month" {
	local title "Probability of cohold period ending"
	}

serrbar mean se month if n ==1, ///
mvopts(mcolor(blue)) /// 
title("`title'", place(left) size(large)) ///
ytitle("") ///
xtitle("") ///
ylabel(, labsize(medium) angle(0) format(%9.3fc)) ///
xlabel(1 "January" 2 "February" 3 "March" 4 "April" 5 "May" 6 "June" 7 "July" 8 "August" 9 "September" 10 "October" 11 "November" 12 "December", ///
	labsize(medium) angle(90) format(%9.0fc)) ///
xscale(range(0.75 12.25))  
graph export "Figures/CohExpl_Inattention_Month_`var'.pdf", replace
graph export "Figures/CohExpl_Inattention_Month_`var'.png", replace
drop mean std obs se n
}

*Probability of coholding to start or end by the th day in month

foreach y in pr_cohold_start pr_cohold_end  {

	if "`y'" == "pr_cohold_start" {
	local title "Probability of coholding period starting"
	local format "%9.3fc"
	}
	else if "`y'" == "pr_cohold_end" {
	local title "Probability of coholding period ending"
	local format "%9.3fc"
	}
	
	local category "ind_emy_seq"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

		foreach var in `category' {
		if "`var'" == "ind_emy_seq" {
			local min 1
			local max 31
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	scatter mean n if subcategory != "", ///
	mcolor(blue) mstyle(p1) ///
	title("`title'", place(left) size(large)) ///
	ytitle("") ///
	xtitle("th day in month", size(large)) ///
	ylabel(, labsize(medium) angle(0) format("`format'")) /// 
	xlabel(0 5 10 15 20 25 30, labsize(medium) angle(0))  ///
	xscale(range(0.25 31.75)) ///
	legend(off)  
	 
drop subcategory mean lqt uqt n 
drop min max  	 
graph export "Figures/CohExpl_Inattention_month_`y'.pdf", replace
graph export "Figures/CohExpl_Inattention_month_`y'.png", replace
}

foreach var in pr_cohold_start_DIM pr_cohold_end_DIM {

		if "`var'" == "pr_cohold_start_DIM" {
		local title "starts"
		}
		else if "`var'" == "pr_cohold_end_DIM" {
		local title "ends"
		}
twoway bar `var' ind_emy_seq, ///
	fcolor(blue) lwidth(medium) lcolor(black) ///
	title("Number of coholding `title' relative to all `title'", place(left) size(large)) ///
	ytitle("") ///
	xtitle("th day in month", size(large)) ///
	ylabel(, labsize(medium) angle(0) format(%15.2fc)) /// 
	xlabel(0 5 10 15 20 25 30, labsize(medium) angle(0))  ///
	xscale(range(0.25 31.75)) ///
	legend(off)  
	
graph export "Figures/CohExpl_Inattention_month_`var'.pdf", replace
graph export "Figures/CohExpl_Inattention_month_`var'.png", replace

}
